#!/bin/bash

root_path=$(cd `dirname $0`;pwd)
sky_path="./3rd/skynet/"
#echo $root_path
source /opt/rh/devtoolset-3/enable 
chmod +x deploy.sh
chmod 777 checker.sh

#检查目录是否存在，不存在就建立
function func_if_mkdir() {
	#mkdir
	dir_=$1
	if  [ -d "$dir_" ]; then
		echo "dir exist="$dir_
	else
		echo "dir not exist="$dir_
		mkdir -p $dir_
	fi
}

#编译lua-packetbase
function func_make_packetbase() {
    cd $root_path
    cd ./3rd/lua-packetbase
    make 
}

#编译lua-espacketbase, IPK底层通信包
function func_make_espacketbase() {
    cd $root_path
    cd ./3rd/lua-espacketbase
    make 
}


#编译lua-memcache
function func_make_memcache() {
    cd $root_path
    cd ./3rd/lua-memcache
    make 
}

#编译lua-memdb
function func_make_memdb() {
    cd $root_path
    cd ./3rd/lua-memdb
    make 
}

#编译cgate
function func_make_cgate() {
    cd $root_path
    cd ./base/csocket/service_csocket_gate/
    make 

    cd $root_path
    cd ./base/csocket/service_ipksocket_gate/
    make 
}

#编译德州比牌工具
function func_make_texas_utils() {
    cd $root_path
    cd ./3rd/lua-texas_utils
    make
}

#编译skynet
function func_make_skynet() {
    cd $root_path
    cd ./3rd/skynet/3rd/
    chmod -R +x ./jemalloc/
    cd $root_path
    cd $sky_path
    make linux
}

# 编译底层依赖的库
function func_build() {
    func_make_skynet
    func_make_packetbase
    func_make_memcache
    func_make_memdb
    func_make_cgate
    func_make_espacketbase
    func_make_texas_utils

    cd $root_path
    cp ./3rd/lua-packetbase/lua_packetbase.so ./3rd/skynet/luaclib/
    cp ./3rd/lua-espacketbase/lua_espacketbase.so ./3rd/skynet/luaclib/ # ipk es包
    cp ./3rd/lua-memcache/lua_memcached.so ./3rd/skynet/luaclib/
    cp ./3rd/lua-memdb/lua_memdb.so ./3rd/skynet/luaclib/
    cp ./3rd/lua-texas_utils/lua_texas_utils.so ./3rd/skynet/luaclib/

    chmod +x ./robot/tarneeb/start.sh
    chmod +x ./robot/tarneeb/stop.sh

    #ln -s ./robot/tarneeb/start.sh ./start.sh
    #ln -s ./robot/tarneeb/stop.sh ./stop.sh
}

# 重新编译底层依赖的库
function func_build_new() {
    cd $root_path
    cd ./3rd/skynet/
    make clean
    cd ../lua-packetbase/
    make clean
    cd ../lua-memcache/
    make clean
    cd ../lua-memdb/
    make clean
    cd ./base/csocket/service_csocket_gate/
    make clean
    cd ./lua-texas_utils/
    make clean
    cd ./base/csocket/service_ipksocket_gate/
    make clean
    func_build
}

if  [ "$1" == "build" ]; then
    func_build
elif  [ "$1" == "rebuild" ]; then
    func_build_new
else
    echo "=======================================  参数选项 ========================================"
    echo "请输入以下参数:  (eg: ./deploy build)"
    echo "build          # 编译底层依赖的库"
    echo "rebuild        # 重新编译底层依赖的库"
    echo "=========================================================================================="
fi
